#include <iostream>
#include <string>

#include "cpp-httplib/httplib.h"
#include "Search.hpp"

using namespace LogArea;

const std::string input = "data/processed_html/processed.txt";
const std::string root_path = "./wwwroot";

int main()
{
    SearchArea::Searcher search;
    search.InitSearch(input);

    httplib::Server svr;

    svr.set_base_dir(root_path);
    svr.Get("/s",[&search](const httplib::Request& req,httplib::Response& rsp){
        if(!req.has_param("word"))
        {
            rsp.set_content("必须要有搜索关键字!","test/plain; charset=utf-8");
            return; 
        }

        // 发起请求
        std::string word = req.get_param_value("word");
        std::cout<<"用户正在搜索: "<<word<<std::endl;

        // 请求结果返回给用户
        std::string json_result;
        search.Search(word,json_result);
        rsp.set_content(json_result,"application/json");
        // rsp.set_content("你好世界!","text/plain; charset=utf-8");
    });

    LogArea::LOG(LogLevel::DEBUG)<<"服务器启动成功!";
    svr.listen("0.0.0.0",8081);
    return 0;
}